package resources;

import java.io.IOException;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;

import daoImpl.DAOStation;
import daoInterface.IDAOStation;
import dto.DTOStation;

import model.Station;

@Path("/station")
public class StationResource 
{
	@Context
	UriInfo uriInfo;
	@Context
	Request request;

	@GET
	@Produces ("application/xml")
	@Path("get/{id}")
	public Station getStation(@PathParam("id") String id)
	{
		IDAOStation daoStation = new DAOStation();
		DTOStation dtoStation = daoStation.getStation(new Integer(id));
		Station station = null;
		
		if (dtoStation.getStationList() == null || dtoStation.getStationList().size() == 0)
		{
			// Dont have station with this specific id
			throw new WebApplicationException(Response.Status.NOT_FOUND);
		}	
		else
			// Found!
			station = dtoStation.getStationList().get(0);
		
		return station;
	}
	@GET
	@Produces ("application/xml")
	@Path("get/{id}/distance/{distance}")
	public List<Station> getNearestStations(@PathParam("id") String id,
												@PathParam("distance") String distance) 
	{
		Station station = getStation(id);
		IDAOStation daoStation = new DAOStation();
		DTOStation dtoStation = daoStation.getNearestStations(station, new Double(distance));
		return dtoStation.getStationList();
	}
	@POST
	@Consumes ("application/xml")
	@Path("/post")
	public String addStation(Station station) throws IOException, ParserConfigurationException, SAXException 
	{		
		//Save to db
		System.out.println(station.toString());
		System.out.println("Add station to db");
		IDAOStation daoStation = new DAOStation();
		daoStation.addStation(station);		
		System.out.println("Done!");
		return "<html> " + "<title>" + "Add Station" + "</title>"
				+ "<body><h1>" + "Station #" + station.getId() + " is added!" + "</body></h1>" + "</html> ";
	}
	@PUT
	@Consumes("application/xml")
	@Path("update/{id}")
	public String updateStation(Station station)
	{
		System.out.println(station);
		IDAOStation daoStation = new DAOStation();
		daoStation.updateStation(station);		
		return null;		
	}
	@DELETE
	@Consumes("application/xml")
	@Path("delete/{id}")
	public String deleteStation(@PathParam("id") String id)
	{		
		IDAOStation daoStation = new DAOStation();
		daoStation.removeStation(new Integer(id));
		return "<html> " + "<title>" + "Delete Station" + "</title>"
		+ "<body><h1>" + "Station #" + id + " is deleted!" + "</body></h1>" + "</html> ";
	}
//	DTOStation dtoStation = daoStation.getStation(new Integer(id));
//	if (dtoStation.getStationList() == null || dtoStation.getStationList().size() == 0)
//		throw new WebApplicationException(Response.Status.NOT_FOUND);
//	Station station = dtoStation.getStationList().get(0);
//	daoStation.removeStation(station);	
	
	
	
	// This method is called if TEXT_PLAIN is request
		@GET
		@Produces(MediaType.TEXT_PLAIN)
		public String sayPlainTextHello() {
			return "You must specify a station id!";
		}

		// This method is called if XML is request
		@GET
		@Produces(MediaType.TEXT_XML)
		public String sayXMLHello() {
			return "<?xml version=\"1.0\"?>" + "<info> You must specify a station id!" + "</info>";
		}

		// This method is called if HTML is request
		@GET
		@Produces(MediaType.TEXT_HTML)
		public String sayHtmlHello() {
			return "<html> " + "<title>" + "Hello Jersey" + "</title>"
					+ "<body><h1>" + "You must specify a station id!" + "</body></h1>" + "</html> ";
		}
}
